package cn.youframe.frame.dao;

import cn.youframe.frame.base.BaseDaoImpl;
import cn.youframe.frame.beans.Menber;
import cn.youframe.frame.beans.YfDeptVO;
import cn.youframe.frame.beans.YfRoleVO;
import cn.youframe.frame.util.SqlBuilder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * User: Administrator
 * DateTime: 12-7-22 下午1:08
 * Desc:
 */
public class LoginDaoImpl extends BaseDaoImpl implements LoginDao {
    @Override
    public int addOnlineUser(Menber menber) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("insert into YF_ONLINE_USER(ACCOUNT,SESSION_ID,ROMOTE_ADDR,SERVER_ADDR)");
        sbSql.append("values(?,?,?,?)");
        sbSql.addArg(menber.getAccount()).addArg(menber.getSessionId());
        sbSql.addArg(menber.getRemoteAddress()).addArg(menber.getServerAddr());
        int ret=getJdbcTemplate().update(sbSql.getSQL(),sbSql.getArgArray());
        return ret;
    }

    @Override
    public int delOnlineUser(String account) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("delete YF_ONLINE_USER where ACCOUNT=?");
        int ret=getJdbcTemplate().update(sbSql.getSQL(),account);
        return ret;
    }

    @Override
    public YfDeptVO getDept(String did) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("select DID,DNAME,DTYPE,MEMO,PDID from YF_DEPT where did=?");
        sbSql.addArg(did);
        YfDeptVO dept=getJdbcTemplate().queryForObject(sbSql.getSQL(),BeanPropertyRowMapper.newInstance(YfDeptVO.class),sbSql.getArgArray());
        return dept;
    }

    @Override
    public List<YfRoleVO> getRoles(String uid) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("select R.RID,R.NAME,R.ENABLE from YF_USER_ROLE UR,YF_ROLE R where UR.RID=R.RID and UR.UID=?");
        sbSql.addArg(uid);
        List<YfRoleVO> rlist=getJdbcTemplate().query(sbSql.getSQL(),BeanPropertyRowMapper.newInstance(YfRoleVO.class),sbSql.getArgArray());
        return rlist;
    }

    @Override
    public Menber getMenber(String account) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("select uid,account,password,username,did,utype,enable,(sysdate-PWD_UPDATE_DATE) PWD_UPDATE_DAYS,");
        sbSql.append("(EXPIRED_DATE-sysdate) EXPIRED_DAYS from yf_user where account=?");
        sbSql.addArg(account);
        Menber menber=getJdbcTemplate().queryForObject(sbSql.getSQL(), BeanPropertyRowMapper.newInstance(Menber.class),sbSql.getArgArray());
        return menber;
    }

    @Override
    public String userLogin(Menber menber) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("insert into YF_LOGIN_LOG(LSEQ,ACCOUNT,IPADDR,SERVER_ADDR)");
        sbSql.append("values(LOGIN_LOG_SEQ.nextval,?,?,?)");
        sbSql.addArg(menber.getAccount()).addArg(menber.getRemoteAddress()).addArg(menber.getServerAddr());
        String key=insertAndGetKey(sbSql.getSQL(),sbSql.getArgArray());
        return key;
    }

    @Override
    public int userLogout(String key) {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("update YF_LOGIN_LOG set LOGOU_TIME=sysdate where LSEQ=? ");
        int ret=getJdbcTemplate().update(sbSql.getSQL(),key);
        return ret;
    }
}
